﻿using System;
using System.Net;
using Netbits.Common.General;

namespace Netbits.Common.Extensions
{
	/// <summary>
	/// Expand the string class
	/// </summary>
	public static class StringExtensions
	{
		#region Public static functions

		/// <summary>
		/// Convert a string to typed enumeration.
		/// </summary>
		/// <typeparam name="T">Type of enumeration</typeparam>
		/// <param name="Object_">String to convert</param>
		/// <returns>Typed enumeration, otherwise exceptions</returns>
		public static T ToEnum<T>( this string Object_ )
		{
			return Converter.ToEnum<T>( Object_ );
		}

		/// <summary>
		/// Convert a string to typed enumeration
		/// </summary>
		/// <typeparam name="T">Type of enumeration</typeparam>
		/// <param name="Object_">String to convert</param>
		/// <param name="DefaultRet_">If any error occurs this value are returned</param>
		/// <returns>Typed enumeration, otherwise DefaultRet_</returns>
		public static T ToEnum<T>( this string Object_, T DefaultRet_ )
		{
			return Converter.ToEnum<T>( Object_, DefaultRet_ );
		}

		/// <summary>
		/// Convert a string to typed value.
		/// </summary>
		/// <typeparam name="TValue">Convert to type</typeparam>
		/// <param name="Value_">Value to convert</param>
		/// <returns>Typed value, otherwise exceptions</returns>
		public static TValue ConvertTo<TValue>( this string Value_ )
		{
			return Converter.ConvertTo<TValue>( Value_ );
		}

		/// <summary>
		/// Convert a string to typed value.
		/// </summary>
		/// <typeparam name="TValue">Convert to type</typeparam>
		/// <param name="Value_">Value to convert</param>
		/// <param name="DefaultRet_">If any error occurs this value are returned</param>
		/// <returns>Typed value, otherwise DefaultRet_</returns>
		public static TValue ConvertTo<TValue>( this string Value_, TValue DefaultRet_ )
		{
			return Converter.ConvertTo<TValue>( Value_, DefaultRet_ );
		}

		/// <summary>
		/// Convert a string to new DateTime instance.
		/// </summary>
		/// <param name="Object_">Value to convert.</param>
		/// <returns>new DateTime instance, otherwise exceptions</returns>
		public static DateTime ToDateTime( this string Object_ )
		{
			return Converter.ToDateTime( Object_ );
		}

		/// <summary>
		/// Convert a string to new DateTime instance.
		/// </summary>
		/// <param name="Object_">Value to convert.</param>
		/// <param name="DefaultRet_">If any error occurs this value are returned</param>
		/// <returns>new DateTime instance, otherwise DefaultRet_</returns>
		public static DateTime ToDateTime( this string Object_, DateTime DefaultRet_ )
		{
			return Converter.ToDateTime( Object_, DefaultRet_ );
		}

		/// <summary>
		/// Convert a string to new Guid instance.
		/// </summary>
		/// <param name="Object_">Value to convert.</param>
		/// <returns>new Guid instance, otherwise exceptions</returns>
		public static Guid ToGuid( this string Object_ )
		{
			return Converter.ToGuid( Object_ );
		}

		/// <summary>
		/// Convert a string to a new Guid instance.
		/// </summary>
		/// <param name="Object_">Value to convert.</param>
		/// <param name="DefaultRet_">If any error occurs this value are returned</param>
		/// <returns>new Guid instance, otherwise DefaultRet_</returns>
		public static Guid ToGuid( this string Object_, Guid DefaultRet_ )
		{
			return Converter.ToGuid( Object_, DefaultRet_ );
		}

		/// <summary>
		/// Convert a string to a new Version instance.
		/// </summary>
		/// <param name="Object_">Value to convert.</param>
		/// <returns>new Version instance, otherwise exceptions</returns>
		public static Version ToVersion( this string Object_ )
		{
			return Converter.ToVersion( Object_ );
		}

		/// <summary>
		/// Convert a string to a new Version instance.
		/// </summary>
		/// <param name="Object_">Value to convert.</param>
		/// <param name="DefaultRet_">If any error occurs this value are returned</param>
		/// <returns>new Version instance, otherwise DefaultRet_</returns>
		public static Version ToVersion( this string Object_, Version DefaultRet_ )
		{
			return Converter.ToVersion( Object_, DefaultRet_ );
		}

		/// <summary>
		/// Convert a string to a new IPAddress instance.
		/// </summary>
		/// <param name="Object_">Value to convert.</param>
		/// <returns>new IPAddress instance, otherwise exceptions</returns>
		public static IPAddress ToIPAddress( this string Object_ )
		{
			return Converter.ToIPAddress( Object_ );
		}

		/// <summary>
		/// Convert a string to a new IPAddress instance.
		/// </summary>
		/// <param name="Object_">Value to convert.</param>
		/// <param name="DefaultRet_">If any error occurs this value are returned</param>
		/// <returns>new IPAddress instance, otherwise DefaultRet_</returns>
		public static IPAddress ToIPAddress( this string Object_, IPAddress DefaultRet_ )
		{
			return Converter.ToIPAddress( Object_, DefaultRet_ );
		}

		/// <summary>
		/// Convert a string "0x01" to an integer.
		/// </summary>
		/// <param name="Object_">Value to convert</param>
		/// <returns>The represent integer, otherwise exceptions</returns>
		public static int HexToInt( this string Object_ )
		{
			return Converter.HexToInt( Object_ );
		}

		/// <summary>
		/// Convert a string "0x01" to an integer
		/// </summary>
		/// <param name="Object_">Value to convert</param>
		/// <param name="DefaultRet_">If any error occurs this value are returned</param>
		/// <returns>The represent integer, otherwise DefaultRet_</returns>
		public static int HexToInt( this string Object_, int DefaultRet_ )
		{
			try
			{
				return Converter.HexToInt( Object_ );
			}
			catch
			{
				return DefaultRet_;
			}
		}

		/// <summary>
		/// Convert an integer to a string like "0x01"
		/// </summary>
		/// <param name="Hex_">Integer to convert.</param>
		/// <returns>The string value for specified integer, otherwise exceptions</returns>
		public static string IntToHex( this int Hex_ )
		{
			return Converter.IntToHex( Hex_ );
		}

		#endregion
	}
}
